// packageI/personalCredit/personalCredit.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    list: [],
    info: [],
    quota: 0,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '个人额度',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getData() {
    wx.showLoading({
      title: '加载中',
    })
    let urls = "plugin.activity-ranking.frontend.quota.personal-quota";
    app._getNetWork({
      url: app.getNetAddresss(urls),
      data: {
        page: this.data.page,
      },
      success: (res) => {
        wx.hideLoading({
          success: (res) => {},
        })
        if (res.data.result == 1) {
          let _data = res.data.data;
          this.setData({
            isLoadMore: true,
            total_page: _data.list.last_page,
            quota: _data.quota,
            list: _data.list.data
          });
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
        } else {
          this.setData({
            page: this.data.page - 1,
            isLoadMore: false
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  _getMoreData() {
    wx.showLoading({
      title: '加载中',
    })
    let urls = "plugin.activity-ranking.frontend.quota.personal-quota";
    if (this.data.page >= this.data.total_page) {
      this.setData({
        isLoadMore: false
      });
      wx.hideLoading({
        success: (res) => {},
      })
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      app._getNetWork({
        url: app.getNetAddresss(urls),
        data: {
          page: this.data.page,
        },
        success: (res) => {
          wx.hideLoading({
            success: (res) => {},
          })
          if (res.data.result == 1) {
            let _data = res.data.data;
            this.setData({
              isLoadMore: true,
              list: this.data.list.concat(_data.list.data)
            });
          } else {
            this.setData({
              page: this.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          wx.hideLoading({
            success: (res) => {},
          })
          console.log(res);
        }
      });
    }
  }
})